//
//  ATVBooleanPropertyViewController.m
//  FlashController
//
//  Created by Martin Schiøth Dyrby on 12/1/11.
//  Copyright 2011 Molamil. All rights reserved.
//

#import "ATVBooleanPropertyViewController.h"
#import "ATVInstanceBooleanProperty.h"

@implementation ATVBooleanPropertyViewController
@synthesize titleLabel;
@synthesize curValue;
@synthesize radioMatrix;
@synthesize isMethod;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateValueHandler:) name:@"updateValue" object:nil];
         curValue = @"false";
    }
    
    return self;
}

- (void) destroy {
}

- (void) updateValueHandler: (NSNotification*) note {
    NSDictionary* dict = (NSDictionary*)note.object;
    NSString* path = [dict valueForKey:@"path"];
    

    if([path isEqualToString:self.path]) {
        NSString* value = [dict valueForKey:@"value"];
        if(value == 0) {
            [radioMatrix selectCellWithTag:0];
        }else if(value == @"1"){
            [radioMatrix selectCellWithTag:1];
        }else{
            [radioMatrix deselectAllCells];
        }
    }
}

- (void) drawFromModel:(ATVInstanceBooleanProperty*) model {
    [titleLabel setStringValue: model.name];
    isMethod = model.isMethod;
    [[self methodButton] setHidden: !isMethod];
}

- (IBAction)radioClickMessage:(NSMatrix*)sender {
    NSButtonCell *selCell = [sender selectedCell];
    if(selCell.tag == 0) {
        curValue = @"false";
    } else {
        curValue = @"true";
    }
    
    NSLog(@"radioClickMessage %@", curValue);
    
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:curValue, @"value", self.path, @"path", nil];

    if(!isMethod){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setBooleanValue" object:dict];
    }
}

- (IBAction)methodMessage:(id)sender {
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:curValue, @"value", self.path, @"path", nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"executeBooleanMethod" object:dict];        
}

@end
